package main

import (
	"cloud.google.com/go/translate"
	"context"
	"flag"
	"fmt"
	"golang.org/x/text/language"
	"google.golang.org/api/option"
	"log"
)

var authJsonFile = flag.String("f", "app.json", "翻译APP认证服务的json")

func main() {
	flag.Parse()
	ctx := context.Background()

	client, err := translate.NewClient(ctx, option.WithCredentialsFile(*authJsonFile))

	if err != nil {
		panic(err)
	}

	text := `中信信托、建信信托收到北京银保监局罚单，均被责令改正并处90万元罚款。违规案由包括：接受银行个人理财资金投资劣后受益权、违规为地方政府提供融资、信政业务开展不合规。（第一财经）`
	target := `en`

	targetText, err := language.Parse(target)
	if err != nil {
		log.Println("Failed to parse target language: %v", err)
		return
	}

	// Translates the text into Russian.
	translations, err := client.Translate(ctx, []string{text}, targetText, nil)
	if err != nil {
		log.Println("Failed to google text: %v", err)
		return
	}

	fmt.Printf("Text: %v\n", text)
	fmt.Printf("Translation: %v\n", translations[0].Text)
}
